package com.byls.controller;

import com.byls.dao.ArticleMapper;
import com.byls.pojo.Articles;
import com.byls.repository.ArticlesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticlesRepository articlesRepository;

    @Autowired
    private ArticleMapper articleMapper;
    @GetMapping("/findall")
    private List<Articles>getAll(){
        return articleMapper.getAllArticle();
    }
    @GetMapping("/findall/{page}/{size}")
    private Page<Articles> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable= PageRequest.of(page-1,size);
        return articlesRepository.findAll(pageable);
    }
    @GetMapping("/findbyid/{id}")
    public Articles findById(@PathVariable("id") Integer id){
        return articlesRepository.findById(id).get();
    }
    @PostMapping("/save")//tong guo RequestBody jiang jsonduixiang zhuanwei java duixiang
    public String save(@RequestBody Articles articles){
        Articles res = articlesRepository.save(articles);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @PutMapping("/update")
    public String update(@RequestBody Articles articles){
        Articles res = articlesRepository.save(articles);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        articlesRepository.deleteById(id);
    }
    @PutMapping("/fileUpload")
    public String upload(HttpServletRequest req){
        Map map=req.getParameterMap();
        for(int i=0;i<map.size();i++){
            System.out.println(map.get(i));
        }
        return "sda";
    }
}
